package com.example.maybeaccount.Mode;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.maybeaccount.MemberChartActivity;
import com.example.maybeaccount.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 成员账单适配器，用于RecyclerView显示（成员图表统计界面）
 */
public class MemberItemAdapter extends RecyclerView.Adapter<MemberItemAdapter.ViewHolder>{
    private List<Item> itemList;
    private Context context;


    private double sumAmount;
    private int mode;
    public static final int MODE_INCOME = 0;
    public static final int MODE_EXPEND = 1;
    public static final int TREE_PARENT = 0;
    public static final int TREE_CHILD = 1;
    private static int treeMode = TREE_PARENT;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        TextView memberText;
        TextView rateText;
        TextView valueText;
        ProgressBar rateBar;
        ImageButton detailButton;
        RecyclerView recyclerViewInside;
        boolean isOpen;

        public ViewHolder(View view) {
            super(view);
            itemView = view;
            memberText = view.findViewById(R.id.member_name);
            rateText = view.findViewById(R.id.rate);
            valueText = view.findViewById(R.id.value);
            rateBar = view.findViewById(R.id.progressbar);
            detailButton = view.findViewById(R.id.details);
            recyclerViewInside = view.findViewById(R.id.recycler_view_inside);
            isOpen = false;
        }
    }

    /**
     * 此处设置点击事件
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(treeMode == TREE_CHILD){
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.frame_count_child, parent, false);
            final ViewHolder holder = new ViewHolder(view);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
            return holder;
        }else {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.frame_menber_chart, parent, false);
            final ViewHolder holder = new ViewHolder(view);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = holder.getAdapterPosition();
                    Item item = itemList.get(position);
                    if(holder.isOpen){
                        //收起子表
                        holder.recyclerViewInside.setVisibility(View.GONE);
                        holder.isOpen = false;
                        holder.recyclerViewInside.addItemDecoration(new SpacesItemDecoration(-2));
                    }else{
                        //展开子表
                        holder.isOpen = true;
                        List<Item> childItemList = ((MemberChartActivity)context).itemManager.getItemsOfMember(item.getMember());
                        ItemAdapter itemAdapter = new ItemAdapter(childItemList,ItemAdapter.CHILD_MODE,context);
                        LinearLayoutManager layoutManager= new LinearLayoutManager(context);
                        holder.recyclerViewInside.setLayoutManager(layoutManager);
                        holder.recyclerViewInside.setAdapter(itemAdapter);
                        //设置各个item之间的间隔
                        int space = 2;
                        holder.recyclerViewInside.addItemDecoration(new SpacesItemDecoration(space));
                        holder.recyclerViewInside.setVisibility(View.VISIBLE);
                    }
                }
            });
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Item item = itemList.get(position);
        DecimalFormat format = new DecimalFormat("#.##");
        float rate;
        holder.memberText.setText(item.getMember());
        if(mode == MODE_INCOME){
            rate = (float)item.getIncome()/(float)sumAmount * 100;
            holder.valueText.setText(format.format(item.getIncome())+"元");
            holder.rateText.setText(format.format(rate)+"%");
            holder.rateBar.setProgress((int)rate);
        }else if(mode == MODE_EXPEND){
            rate = (float)item.getExpend()/(float)sumAmount * 100;
            holder.valueText.setText(format.format(item.getExpend())+"元");
            holder.rateText.setText(format.format(rate)+"%");
            holder.rateBar.setProgress((int)rate);
        }
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public MemberItemAdapter(List<Item> itemList, int mode,  Context context) {
        this.itemList = itemList;
        this.mode = mode;
        this.context = context;
        sumAmount = 0;
        if(mode == MODE_EXPEND){
            for(int i = 0;i < itemList.size();i ++){
                sumAmount += itemList.get(i).getExpend();
            }
        }else if(mode == MODE_INCOME){
            for(int i = 0;i < itemList.size();i ++){
                sumAmount += itemList.get(i).getIncome();
            }
        }

    }

    private void addChildItems(String parentName,int pos) {
        //childItemList = ((MemberChartActivity)context).itemManager.getItemOfMember(parentName);
        //itemList.addAll(pos+1,childItemList);
        //treeMode = TREE_CHILD;

    }

}
